package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.pojo.Brand;
import com.itheima.restkeeper.mapper.BrandMapper;
import com.itheima.restkeeper.req.BrandVo;
import com.itheima.restkeeper.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description：品牌管理 服务实现类
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Override
    public Page<Brand> findBrandVoPage(BrandVo brandVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<Brand> brandPage = new Page<>(pageNum,pageSize);
        //构建查询条件
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        //按品牌名称查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getBrandName())) {
            queryWrapper.eq(Brand::getBrandName,brandVo.getBrandName());
        }
        //按品牌分类查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getCategory())) {
            queryWrapper.eq(Brand::getCategory,brandVo.getCategory());
        }
        //按品牌状态查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getEnterpriseId())) {
            queryWrapper.eq(Brand::getEnterpriseId,brandVo.getEnterpriseId());
        }
        //按创建时间降序
        queryWrapper.orderByDesc(Brand::getCreatedTime);
        //执行分页查询
        Page<Brand> page = page(brandPage, queryWrapper);
        return page;
    }

    @Override
    public Brand createBrand(BrandVo brandVo) {
        //转换BrandVo为Brand
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        //保存brand
        boolean result = save(brand);
        if (result) {
            return brand;
        }
        return null;
    }

    @Override
    public Boolean updateBrand(BrandVo brandVo) {
        //转换BrandVo为BrandB
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        //保存
        boolean result = updateById(brand);
        return result;
    }

    @Override
    public Boolean deleteBrand(String[] checkedIds) {
        //转换数组为集合
        List<String> list = Arrays.asList(checkedIds);
        ArrayList<Long> arrayList = new ArrayList<>();
        list.forEach(a->
                arrayList.add(Long.valueOf(a)));
        //批量删除
        boolean result = removeByIds(arrayList);
        return result;
    }

    @Override
    public List<Brand> findBrandVoList() {
        //构建查询条件
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Brand::getEnableFlag,SuperConstant.YES);
        //查询有效状态
        List<Brand> result = list(queryWrapper);
        return result;
    }
}
